/*
 * @Author: silence
 * @Date: 2023-06-30 14:56:29
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-26 16:49:20
 * @Description: 首页-com.lenercom.emsneutral
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../utils/color/ColorUtils.dart';
import '../../Mine/Language/SilLocalizations/SilLocalizations.dart';
import '../../PowerStation/Controller/PowerStationController.dart';
import 'HomeEnergyItem.dart';

class HomeSystermStateView extends StatefulWidget {
  const HomeSystermStateView({super.key});

  @override
  State<HomeSystermStateView> createState() => _HomeSystermStateViewState();
}

class _HomeSystermStateViewState extends State<HomeSystermStateView> {
  PowerStationController _powerStationController =
      Get.put(PowerStationController());

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(bottom: 40.w, left: 20.w, right: 20.w),
      child: contentView(),
    );
  }

  contentView() {
    return Column(
      children: [
        /// 标题
        Obx(() => titleView()),

        /// 系统能耗图
        Stack(
          alignment: AlignmentDirectional.center,
          children: [
            /// 输入输出
            systemEnergyDiagramView(),

            /// 储能机
            Obx(() => centerView())
          ],
        )
      ],
    );
  }

  titleView() {
    String day =
        _powerStationController.powerStationEntity.value.runTime ?? '0';

    /// 运行多少天
    String runDaysStr =
        '$day ${LangOf(context)?.index?.days} ${LangOf(context)?.index?.online}';
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SilButton(
          useConstrained: true,
          imgPath: 'imgs/home/home_xt.png',
          imgHeight: 25.w,
          imgWidth: 25.w,
          title: LangOf(context)?.index?.system_status,
          titleColor: ColorUtils.color_font_main,
          fontSize: 16.sp,
          imagePosition: ImagePosition.Left,
          margin: EdgeInsets.only(right: 10.w),
          maxLines: 0,
        ),
        Expanded(
            child: SilText(
          runDaysStr,
          color: ColorUtils.color_font_main,
          fontSize: 14.sp,
          maxLines: 0,
          textAlign: TextAlign.right,
        )),
      ],
    );
  }

  /// 系统能耗图
  systemEnergyDiagramView() {
    return Container(
      margin: EdgeInsets.only(top: 20.w),
      height: 345.w,
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: [
          /// 光伏板
          Positioned(
            top: 0,
            left: 0,
            child: Obx(() => topLeftView()),
          ),

          /// 电网
          Positioned(
            top: 0,
            right: 0,
            child: Obx(() => topRightViwe()),
          ),

          /// 电池
          Positioned(
            bottom: 0,
            left: 0,
            child: Obx(() => bottomLeftView()),
          ),

          /// 负载
          Positioned(
            bottom: 0,
            right: 0,
            child: Obx(() => bottomRightView()),
          ),
        ],
      ),
    );
  }

  /// 光伏板
  topLeftView() {
    String value = _powerStationController.powerStationEntity.value.pvDataStr;
    String lineGifPath =
        _powerStationController.powerStationEntity.value.pvStateGifImgPath;

    return HomeEnergyItem(
      title: LangOf(context)?.index?.PV,
      imgPath: 'imgs/home/home_1.png',
      value: '${value}kW',
      lineGifPath: lineGifPath,
    );
  }

  /// 电网
  topRightViwe() {
    String value =
        _powerStationController.powerStationEntity.value.powerGridDataStr;
    String lineGifPath = _powerStationController
        .powerStationEntity.value.powerGridStateGifImgPath;

    return HomeEnergyItem(
      title: LangOf(context)?.index?.grid,
      imgPath: 'imgs/home/home_2.png',
      value: '${value}kW',
      lineGifPath: lineGifPath,
      position: HomeEnergyPosition.TopRight,
    );
  }

  /// 电池
  bottomLeftView() {
    String value =
        _powerStationController.powerStationEntity.value.batteryDataStr;
    String lineGifPath =
        _powerStationController.powerStationEntity.value.batteryStateGifImgPath;

    return HomeEnergyItem(
      title: LangOf(context)?.index?.battery_level,
      imgPath: 'imgs/home/home_3.png',
      value: '${value}kW',
      lineGifPath: lineGifPath,
      position: HomeEnergyPosition.BottomLeft,
    );
  }

  /// 负载
  bottomRightView() {
    String value = _powerStationController.powerStationEntity.value.loadDataStr;
    String lineGifPath = _powerStationController
        .powerStationEntity.value.loadStateStateGifImgPath;

    String onGridLoad = LangOf(context)?.index?.on_grid_load ?? "";
    String onGridLoadStr =
        _powerStationController.powerStationEntity.value.onGridLoadStr;
    String offGridLoad = LangOf(context)?.index?.off_grid_load ?? "";
    String offGridLoadStr =
        _powerStationController.powerStationEntity.value.offGridLoadStr;

    return SilEmptyButton(
      onTapFunc: () {
        SilToast.show(
            '${onGridLoad} ${onGridLoadStr}kW,\n${offGridLoad} ${offGridLoadStr}kW',
            duration: 3000,
            position: ToastPosition.Bottom);
      },
      useConstrained: true,
      child: HomeEnergyItem(
        title: LangOf(context)?.index?.load,
        imgPath: 'imgs/home/home_4.png',
        value: '${value}kW',
        lineGifPath: lineGifPath,
        position: HomeEnergyPosition.BottomRight,
        isShowDetailFlag: true,
      ),
    );
  }

  /// 储能机
  centerView() {
    bool isRuning = _powerStationController.powerStationEntity.value.isRuning;
    return Container(
        child: Stack(
      alignment: AlignmentDirectional.center,
      children: [
        SilImage(
          isRuning
              ? 'imgs/home/home_center1.png'
              : 'imgs/home/home_center2.png',
          height: 100.w,
          width: 100.w,
        ),
        Positioned(
            top: 55.w,
            child: Container(
              width: 90.w,
              height: 38.w,
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              alignment: Alignment.center,
              child: SilText(
                LangOf(context)?.index?.hybrid,
                color: ColorUtils.color_theme,
                fontSize: 13.sp,
                maxLines: 0,
                textAlign: TextAlign.center,
              ),
            )),
      ],
    ));
  }
}
